import Vue from 'vue'

import core from 'gapi/core'

import App from './App'

import widget from 'gapi/widget'
import cache from 'gapi/cache.js'
import * as util from 'js/util.js'
import commonPage from 'clientApi/commonPage.js'


let pageDate = {}
let img = ""
let contendText = ''
new core('dmsass').start(() => {

	new Vue({
		el: 'body',
		data:{
			map:{},
			positionList:{}
		},
		components: {
			App
		},
		methods:{
			init(){
				cache.getCache('KqCacheMessge',(data)=>{
                    if(data){
                        pageDate.project_id = data.project_id || util.getUrddataString('project_id')
                        pageDate.team_id = data.team_id || util.getUrddataString('team_id')
                    }
                    pageDate.attendance_id = util.getUrddataString('attendance_id')
                    pageDate.task_id = util.getUrddataString('task_id')
                    this.getDate()
                })
			},
			setPosition(){
				let coordinate = this.positionList.attend_position.coordinate.split(',')
				this.map = new BMap.Map('l-map')
				this.map.centerAndZoom(new BMap.Point(coordinate[0], coordinate[1]), 15)

				// 设置考勤地点
				console.log(this.positionList.status)
				if(this.positionList.status){
					img = util.KqMapImg.attendImgN
				}else{
					img = util.KqMapImg.attendImgU
				}
				contendText = '<span class="signin-tip">打卡位置<span class="arrow"></span></span>'
				let myIcon = new BMap.Icon(img, new BMap.Size(24, 32),{
					imageSize:new BMap.Size(24,32)
				})
				this.setPoint(this.positionList.attend_position.coordinate,myIcon,contendText)

				//设置任务地点
				contendText = '<span class="signin-tip location">考勤标准位置<span class="arrow"></span></span>'
				img = util.KqMapImg.taskImg
				myIcon = new BMap.Icon(img, new BMap.Size(30, 40),{
					imageSize:new BMap.Size(30,40)
				})
				this.positionList.task_position.forEach((item)=>{
					this.setPoint(item.coordinate,myIcon,contendText)
				})
			},
			
			getDate(){
				console.log(pageDate)
				 commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/attendance/attendance_position',
                    data: {
                        team_id:pageDate.team_id,
                        project_id: pageDate.project_id,
                        attendance_id:pageDate.attendance_id,
                       	task_id:pageDate.task_id
                    },
                    success: (res) => {
                        console.log(res)
                        // res = JSON.parse(res)
                        commonPage.loadPageStatus('loadSuccess')
                        if(res.errno==0){
                            this.positionList = res.data
                            this.setPosition()
                        }else{
                            widget.toast('获取数据失败')
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
			},
			//打点
			setPoint(item,myIcon,contendText){
				let coordinate = item.split(",")
				let lng = coordinate[0]
				let lat = coordinate[1]
				let point = new BMap.Point(lng,lat)
				var marker = new BMap.Marker(point,{icon: myIcon}) // 创建标注
				this.map.addOverlay(marker)
				var opts = {
					position : point,    // 指定文本标注所在的地理位置
					offset   : new BMap.Size(-20, -35)    //设置文本偏移量
				}
				var label = new BMap.Label('',opts)  // 创建文本标注对象
				label.setStyle({
					border: null,
					padding:0
				})
				label.setContent(contendText)
				marker.setLabel(label)
			}
		},
		ready(){
			// 
			this.init()
		}
	})

})